package com.campus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.entity.*;
import com.campus.entity.Clazz.ClazzTeacher;
import com.campus.mapper.*;
import com.campus.service.AccountMapOpenidService;
import com.campus.util.SendMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【account_map_openid】的数据库操作Service实现
* @createDate 2023-04-05 13:36:30
*/
@Service
public class AccountMapOpenidServiceImpl extends ServiceImpl<AccountMapOpenidMapper, AccountMapOpenid>
    implements AccountMapOpenidService{

    @Autowired
    private AccountMapOpenidMapper mapOpenidMapper;

    @Autowired
    private SendMsg sendMsg;

    @Autowired
    private DormStudentViewMapper dormStudentViewMapper;
    @Autowired
    private ClazzStudentViewMapper clazzStudentMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private ClazzTeacherMapper clazzTeacherMapper;

    @Override
    public ResultBody sendToPerson(String account, Map<String, String> infos, String path) {
        sendMsg.sendMsg(Collections.singletonList(queryOpenid(account)),infos,path);
        return ResultBody.ok();
    }

    @Override
    public ResultBody sendFromCommittee(String account, Map<String, String> infos, String path) {
        ClazzStudentView one = clazzStudentMapper.selectOne(new QueryWrapper<ClazzStudentView>().eq("account", account));
        List<ClazzStudentView> students = clazzStudentMapper.selectList(new QueryWrapper<ClazzStudentView>().eq("class_id", one.getClassId()));
        List<String> ids = students.stream().map(ClazzStudentView::getAccount).collect(Collectors.toList());
         sendMsg.sendMsg(getOpenids(ids),infos,path);
        return ResultBody.ok();
    }

    @Override
    public ResultBody sendFromInstructor(String account, Map<String, String> infos, String path) {
        Teacher teacher = teacherMapper.selectOne(new QueryWrapper<Teacher>().eq("account", account));
        List<ClazzTeacher> clazzs = clazzTeacherMapper.selectList(new QueryWrapper<ClazzTeacher>().eq("teacher_id", teacher.getId()));
        for(ClazzTeacher ct:clazzs){
            List<ClazzStudentView> students = clazzStudentMapper.selectList(new QueryWrapper<ClazzStudentView>().eq("class_id", ct.getClassId()));
            List<String> ids = students.stream().map(ClazzStudentView::getAccount).collect(Collectors.toList());
            sendMsg.sendMsg(getOpenids(ids),infos,path);
        }
        return ResultBody.ok();
    }

    @Override
    public ResultBody sendFromDormAdmin(String account, Map<String, String> infos, String path) {
        List<DormStudentView> stu = dormStudentViewMapper.selectList(new QueryWrapper<DormStudentView>().eq("building_admin", account));
        List<String> ids = stu.stream().map(DormStudentView::getAccount).collect(Collectors.toList());
        sendMsg.sendMsg(getOpenids(ids),infos,path);
        return ResultBody.ok();
    }

    /**
     * Openid映射表中添加数据,会进行重复判断
     * @param account
     * @param openid
     * @return
     */
    @Override
    public ResultBody add(String account, String openid) {
        synchronized (account.intern()){
            if (isExist(account)){
                return ResultBody.error();
            }
            AccountMapOpenid map = new AccountMapOpenid(account, openid);
            int insert = mapOpenidMapper.insert(map);
            if (insert==1){
                return ResultBody.ok();
            }else{
                return ResultBody.error();
            }
        }
    }

    /**
     * 根据Account查询对应的Openid
     * @param account
     * @return
     */
    private String queryOpenid(String account){
        AccountMapOpenid account1 = mapOpenidMapper.selectOne(new QueryWrapper<AccountMapOpenid>().eq("account", account));
        if (account1==null)return null;
        return account1.getOpenid();
    }
    private boolean isExist(String account) {
        List<AccountMapOpenid> account1 = mapOpenidMapper.selectList(new QueryWrapper<AccountMapOpenid>().eq("account", account));
        return !account1.isEmpty();
    }
    private List<String> getOpenids(List<String> students){
        List<String> ids = new ArrayList<>();
        for(String stu:students){
            String s = queryOpenid(stu);
            if(s!=null){
                ids.add(s);
            }

        }
        return ids;
    }

}




